package editor;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.geom.AffineTransform;

import javax.swing.*;

public class UI extends JFrame implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	
	private JMenuBar menu;
	private JMenu file;
	private JMenu tiles;
	
	private JMenuItem newLevel;
	private JMenuItem saveLevel;
	private JMenuItem loadLevel;
	private JMenuItem exit;
	
	private JMenuItem newTileSet;
	
	private View panel;
	
	private TilesetDisplay display;
	
	private Level current;
	
	private int selectedTile;

	public UI()
	{
		this.setSize( 500, 500 );
		
		menu = new JMenuBar();
		
		file = new JMenu( "File" );
		tiles = new JMenu( "Tile Options" );
		menu.add( file );
		menu.add( tiles );
		
		newLevel = new JMenuItem( "New Level" );
		saveLevel = new JMenuItem( "Save Level" );
		loadLevel = new JMenuItem( "Load Level" );
		exit = new JMenuItem( "Exit" );
		file.add( newLevel );
		file.add( saveLevel );
		file.add( loadLevel );
		file.add( exit );
		
		newTileSet = new JMenuItem( "New Tileset" );
		tiles.add( newTileSet );
		
		panel = new View();
		panel.setPreferredSize( new Dimension( 32 * 10, 32 * 10 ) );
		
		newLevel.addActionListener( this );
		saveLevel.addActionListener( this );
		loadLevel.addActionListener( this );
		newTileSet.addActionListener( this );
		exit.addActionListener( this );
		
		this.setJMenuBar( menu );
		
		this.add( panel );
		this.pack();
		
		display = new TilesetDisplay( this );
		
		selectedTile = 0;
		
		this.setDefaultCloseOperation( EXIT_ON_CLOSE );

		this.setVisible( true );
	}

	public void actionPerformed( ActionEvent e ) 
	{
		Object o = e.getSource();
		if( o.equals( newLevel ) )
		{
			LevelDialog ld = new LevelDialog( this, display );
			
			if( ld.getLevel() != null )
				current = ld.getLevel();
			
			panel.repaint();
		}
		else if( o.equals( saveLevel ) )
		{
			if( current != null )
			{
				current.save();
			}
		}
		else if( o.equals( loadLevel ) )
		{
			Level l = Level.load( this );
			if( l != null )
			{
				current = l;
				display.addTileset( current.getTileset() );
				panel.repaint();
			}
		}
		else if( o.equals( newTileSet ) )
		{
			new TilesetDialog( this );
		}
		else if( o.equals( exit ) )
		{
			System.exit( 0 );
		}
	}
	
	public void setSelectedTile( int i )
	{
		selectedTile = i;
	}
	
	public static void main( String[] args )
	{
		new UI();
	}
	
	private class View extends JPanel implements MouseListener, MouseMotionListener
	{
		private int x, y;
		
		public View()
		{
			this.addMouseListener( this );
			this.addMouseMotionListener( this );
		}
		
		public void paintComponent( Graphics g )
		{
			g.setColor( Color.white );
			g.fillRect( 0, 0, this.getWidth(), this.getHeight() );
			
			if( current != null )
			{
				current.render( display.currentLayer(), g );
				
				current.getTileset().getTileWithKey( selectedTile ).getImage().draw( g, AffineTransform.getTranslateInstance( x, y ) );
			}
		}

		public void mouseClicked( MouseEvent e ) 
		{
			try
			{
				int x1 = e.getX() / 32;
				int y1 = e.getY() / 32;
				
				if( current != null )
				{
					current.setTile( display.currentLayer(), x1, y1, selectedTile );
					this.repaint();
				}
			}
			catch( ArrayIndexOutOfBoundsException err ){}
			
		}

		public void mouseEntered( MouseEvent e ) {}

		public void mouseExited( MouseEvent e ) {}
		
		public void mousePressed( MouseEvent e ) {}

		public void mouseReleased( MouseEvent e ) {}

		public void mouseDragged( MouseEvent e ) 
		{
			try
			{
				int x1 = e.getX() / 32;
				int y1 = e.getY() / 32;
			
				if( current != null )
				{
					current.setTile( display.currentLayer(), x1, y1, selectedTile );
					this.repaint();
				}
			
				repaint();
			}
			catch( ArrayIndexOutOfBoundsException err ){}
		}

		public void mouseMoved( MouseEvent e ) 
		{
			x = ((int)(e.getX() / 32))*32;
			y = ((int)(e.getY() / 32))*32;
			
			repaint();
		}
	}
}
